package com.purong.purong.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.purong.purong.R;


/**
 * Created by wenjiezeng on 2017/9/29.
 * 自定义加载状态控件
 */
public class LoadingView extends LinearLayout {
    private Context mContext;
    private ImageView mImageView,noData;
    private LinearLayout loadFail,noResult;
    private TextView reload,refresh,noResultTv;
    private AnimationDrawable animationDrawable;
    private OnLoadListener mListener;
    private int color;
    private int noDataImg;
    public LoadingView(Context context) {
        super(context);
        this.mContext=context;
        initView();
    }
    //设置无数据时显示的提示文字和刷新按钮文字
    public void setNoResultToast(String noResultToast,String refreshToast){
        if (noResultToast!=null&&!noResultToast.equals("")){
            noResultTv.setText(noResultToast);
        }
        if (refreshToast!=null&&!refreshToast.equals("")){
            refresh.setText(refreshToast);
        }
    }
    public void setLoadListener(OnLoadListener listener){
        this.mListener=listener;
    }
    public LoadingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext=context;
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.loading_view);
        color = ta.getResourceId(R.styleable.loading_view_bg_color, 0);
        noDataImg=ta.getResourceId(R.styleable.loading_view_no_data_img, 0);
        ta.recycle();
        initView();
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext=context;
        initView();
    }


    private void initView() {
//    View view= LayoutInflater.from(mContext).inflate(R.layout.loading_view_layout,this);     //一定要把布局添加进容器，不能为null
//        addView(view);
        View.inflate(mContext, R.layout.loading_view_layout, this);
        mImageView = (ImageView)findViewById(R.id.image_view);
        loadFail=(LinearLayout)findViewById(R.id.load_fail);
        reload=(TextView)findViewById(R.id.reload);
        noResult=(LinearLayout)findViewById(R.id.no_result);
        noResultTv=(TextView) findViewById(R.id.no_result_toast);
        refresh=(TextView)findViewById(R.id.refresh);
        noData=(ImageView)findViewById(R.id.no_data);
        if (noDataImg!=0){
            noData.setImageResource(noDataImg);
        }
        if (color!=0)
        findViewById(R.id.main_lay).setBackgroundResource(color);
        findViewById(R.id.main_lay).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        reload.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mListener!=null){
                    loading();
                    mListener.onReload();
                }
            }
        });
        refresh.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                if (mListener!=null){
                    loading();
                    mListener.onRefresh();
                }
            }
        });
        animationDrawable = ((AnimationDrawable) mImageView.getDrawable());
        animationDrawable.start();
    }
    //加载中
    public void loading(){
        super.setVisibility(VISIBLE);
        mImageView.setVisibility(VISIBLE);
        loadFail.setVisibility(GONE);
        noResult.setVisibility(GONE);
        startAnim();
    }
    //加载完成
    public void finish(){
        super.setVisibility(GONE);
        stopAnim();
    }
    //加载失败
    public void fail(){
        super.setVisibility(VISIBLE);
        mImageView.setVisibility(GONE);
        loadFail.setVisibility(VISIBLE);
        noResult.setVisibility(GONE);
        stopAnim();
    }
    //无数据，不带刷新按钮
    public void noResult(){
        super.setVisibility(VISIBLE);
        mImageView.setVisibility(GONE);
        loadFail.setVisibility(GONE);
        noResult.setVisibility(VISIBLE);
        refresh.setVisibility(GONE);
        stopAnim();
    }
    //无数据，带刷新按钮
    public void noResultWithRefresh(){
        super.setVisibility(VISIBLE);
        mImageView.setVisibility(GONE);
        loadFail.setVisibility(GONE);
        noResult.setVisibility(VISIBLE);
        refresh.setVisibility(VISIBLE);
        stopAnim();
    }
    @Override
    public void setVisibility(int visibility) {

        super.setVisibility(visibility);
        if (visibility == GONE || visibility == INVISIBLE) {
            stopAnim();
        } else {
            startAnim();
        }

    }
    public void startAnim() {
//        if (animationDrawable == null) {
//            initView();
//        }

        if (animationDrawable != null) {
            animationDrawable.start();
        }
    }

    public void stopAnim() {
//        if (animationDrawable == null) {
//            initView();
//        }

        if (animationDrawable != null) {
            animationDrawable.stop();
        }
    }
    public interface OnLoadListener {
        void onReload();
        void onRefresh();
    }
}
